<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class CI_Music{
	var $CI;
	public function __construct( ){
		$this->CI =& get_instance();
	}
	
    function getListRecords( ){
		$rs = $this->CI->db->get( TBL_MUSIC_GENRE )->result();
		return $rs;
	}
	
	function getMusicRecordBy_id($id){
		return $this->CI->db->where('id',$id)->get(TBL_MUSIC_GENRE)->result();
	}
	
	function getMusicRecordBy_genre($genre){
		return $this->CI->db->where('genre',$genre)->get(TBL_MUSIC_GENRE)->result();
	}
	
	function deleteRecord_id($id){
		$this->CI->db->where('id',$id)->delete(TBL_MUSIC_GENRE);
		return true;
	}
	
	//admin
	function saveMusicName($id, $name){
		if($id == 0){
			$recordRS = $this->getMusicRecordBy_genre($name);
			if(!empty($recordRS)){
				return false;
			}
			//create new
			$this->CI->db->insert( TBL_MUSIC_GENRE, array('genre'=> $name) );
			return true;
		}else{
			$recordRS = $this->getMusicRecordBy_genre($name);
			if(count($recordRS)<= 1 AND $recordRS[0]->id == $id){
				$this->CI->db->where('id',$id)->update( TBL_MUSIC_GENRE, array('genre'=> $name) );
				return true;
			}else{
				return false;
			}
		}
	}
	
	
}

?>